//
//  AppRecommandViewController.m
//  upnpxdemo
//
//  Created by 赵帅 on 13-9-3.
//  Copyright (c) 2013年 wiimu.inc. All rights reserved.
//

#import "AppRecommandViewController.h"
#import "RecommendApp.h"

@interface AppRecommandViewController ()
{
    NSMutableArray * appArray;
}

@property (retain) NSMutableArray * appArray;

@end

@implementation AppRecommandViewController
@synthesize appArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self getRecommendApp];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
   
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return NSLocalizedString(@"RECOMMEND",@"");
}

#pragma mark -
#pragma mark private methods
-(void)getRecommendApp
{
    self.appArray = [NSMutableArray array];
    
    NSString * path = [[NSBundle mainBundle] bundlePath];
    path = [path stringByAppendingPathComponent:@"RecommendApp.plist"];
    NSArray * tempArray = [NSArray arrayWithContentsOfFile:path];
    
    for(NSDictionary * dic in tempArray)
    {
        RecommendApp * app = [[RecommendApp alloc] init];
        app.name = [dic objectForKey:@"name"];
        app.desc = [dic objectForKey:@"desc"];
        app.url_iphone = [dic objectForKey:@"iphone_url"];
        app.url_ipad = [dic objectForKey:@"ipad_url"];
        app.image = [dic objectForKey:@"image"];
        [appArray addObject:app];
    }
}

#pragma mark -
#pragma mark UITableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [appArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    else
    {
        for(UIView * subview in [cell.contentView subviews])
        {
            [subview removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    // Configure the cell.
    
    RecommendApp * app = [appArray objectAtIndex:indexPath.row];
    
    UIImageView * imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:app.image]];
    [cell.contentView addSubview:imageview];
    [imageview setFrame:CGRectMake(10, 10, 40, 40)];
    
    UILabel * label = [[UILabel alloc] init];
    label.text = app.name;
    label.backgroundColor = [UIColor clearColor];
    label.frame = CGRectMake(60, 10, 200, 40);
    [cell.contentView addSubview:label];
    
    //描述
    CGSize maxSize = CGSizeMake(tableView.frame.size.width-50, 45);
    CGSize size = [app.desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize lineBreakMode:NSLineBreakByTruncatingTail];
    
    UILabel * descLabel = [[UILabel alloc] init];
    descLabel.text = app.desc;
    descLabel.numberOfLines = 0;
    descLabel.font = [UIFont systemFontOfSize:14];
    descLabel.textColor = [UIColor darkGrayColor];
//    descLabel.backgroundColor = [UIColor redColor];
    descLabel.frame = CGRectMake(10, 55, size.width, size.height);
    [cell.contentView addSubview:descLabel];
    
    UIImageView * rightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rightarrow1.png"]];
    rightImage.frame = CGRectMake(tableView.frame.size.width-30, 40, 10, 20);
    [cell.contentView addSubview:rightImage];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendApp * app = [appArray objectAtIndex:indexPath.row];
    
    if(NSClassFromString(@"SKStoreProductViewController")) { // Checks for iOS 6 feature.
        
        SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];
        storeProductVC.delegate = self;
        
        NSDictionary *dict = [NSDictionary dictionaryWithObject:[app getAppID] forKey:SKStoreProductParameterITunesItemIdentifier];
        [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error) {
            if (result) {
                [self presentViewController:storeProductVC animated:YES completion:nil];
            }  
        }];
    } else { // Before iOS 6, we can only open the URL
        [[UIApplication sharedApplication] openURL:[app getAppstoreURL]];
    }
    
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [viewController dismissModalViewControllerAnimated:YES];
}

@end
